package com.kgc.controller;

import com.kgc.service.IProductProviderService;
import entity.Product;
import entity.ResponseMessage;
import entity.StatusCode;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import service.product.productservice.ProductAPI;


@Controller
public class ProductProviderController implements ProductAPI {


    private Logger logger = Logger.getLogger(this.getClass());

    @Autowired
    private IProductProviderService iProductProviderService;


    /**
     * 添加一条商品信息
     * @param product
     * @return ResponseMessage
     */
    @Override
    @RequestMapping("insertProduct")
    @ResponseBody
    public ResponseMessage insertProduct(@RequestBody Product product) {
        logger.info("ProductProviderController insertProduct is start ......params: product-->" + product);
        //判断一下参数
        if (product == null || "".equals(product)) {
            logger.error("ProductProviderController insertProduct is error ,The param is null");
            ResponseMessage responseMessage = new ResponseMessage();
            responseMessage.setMessage("传输的对象为空");
            responseMessage.setCode(StatusCode.ERROR);
        }
        ResponseMessage responseMessage =  iProductProviderService.insertProduct(product);
        logger.debug("ProductProviderController insertProduct result :responseMessage--> " + responseMessage);
        return responseMessage;
    }


    /**
     * 异步检查是否重名
     * @param pname
     * @return ResponseMessage
     */
    @Override
    @RequestMapping("insertPnameCheck")
    @ResponseBody
    public ResponseMessage insertPnameCheck(@RequestParam("String") String pname) {
        logger.info("ProductProviderController insertPnameCheck is start ......params: panme-->" + pname);
        //判断一下参数
        if (pname == null || "".equals(pname)) {
            logger.error("ProductProviderController insertPnameCheck is error ,The param is null");
            ResponseMessage responseMessage = new ResponseMessage();
            responseMessage.setMessage("传输的值为空");
            responseMessage.setCode(StatusCode.ERROR);
        }
        ResponseMessage responseMessage =  iProductProviderService.insertPnameCheck(pname);
        logger.debug("ProductProviderController insertProduct result :responseMessage--> " + responseMessage);
        return responseMessage;
    }

    /**
     * 查询所有加工工艺信息
     * @return ResponseMessage
     */
    @Override
    @RequestMapping("selectMachining")
    @ResponseBody
    public ResponseMessage selectMachining(){
        logger.info("ProductProviderController selectMachining is start ....");
        ResponseMessage responseMessage =  iProductProviderService.selectMachining();
        logger.debug("ProductProviderController selectMachining result :responseMessage--> " + responseMessage);
        return responseMessage;
    }

    /**
     * 查询所有供应商的 id 和 公司名称 信息
     * @return responseMessage
     */
    @Override
    @RequestMapping("selectProviderInfo")
    @ResponseBody
    public ResponseMessage selectProviderInfo(){
        logger.info("ProductProviderController selectProviderInfo is start ....");
        ResponseMessage responseMessage =  iProductProviderService.selectProviderInfo();
        logger.debug("ProductProviderController selectProviderInfo result :responseMessage--> " + responseMessage);
        return responseMessage;
    }


    /**
     * 分页查询商品信息,联表分类表和供应商表
     * @return ResponseMessage
     */
    @Override
    @RequestMapping("selectInfoByProductCategoryProvider")
    @ResponseBody
    public ResponseMessage selectInfoByProductCategoryProvider(
            @RequestParam(value = "providerId",required = false) String providerId,
            @RequestParam(value = "pname",required = false) String pname,
            @RequestParam(value ="type",required = false) String type,
            @RequestParam(value ="status",required = false,defaultValue = "-1") String status,
            @RequestParam(value = "sort",defaultValue = "DESC") String sort,
            @RequestParam(value = "pageNum",defaultValue = "") int pageNum

    ){
        logger.info("ProductProviderController selectInfoByProductCategoryProvider is start ......params:providerId-->"+
                providerId+",pname:-->"+pname+",type:->"+type+",status:->"+status+",sort:->"+sort+",pageNum:->"+pageNum);
        ResponseMessage responseMessage =  iProductProviderService.selectInfoByProductCategoryProvider(providerId,pname,type,status,sort,pageNum);
        logger.debug("ProductProviderController selectInfoByProductCategoryProvider result :responseMessage--> " + responseMessage);
        return responseMessage;
    }


    /**
     * 通过id查询一条商品信息
     * @param id
     * @return  ResponseMessage
     */
    @Override
    @RequestMapping("selectProductById")
    @ResponseBody
    public ResponseMessage selectProductById(@RequestParam("id") String id){
        logger.info("ProductProviderController selectProductById is start ....param:id-->"+id);
        //检查下参数,如果为空则返回
        if (id == null || "".equals(id)) {
            ResponseMessage responseMessage = new ResponseMessage();
            responseMessage.setCode(StatusCode.ERROR);
            responseMessage.setMessage("The id is null ,please check param");
            logger.error("ProductProviderController selectProductById is error,The Param : Id  is null");
            return responseMessage;
        }
        ResponseMessage responseMessage =  iProductProviderService.selectProductById(id);
        logger.debug("ProductProviderController selectProductById result:responseMessage"+responseMessage);
        return responseMessage;
    }

    /**
     * 更新一条商品信息
     * @param product
     * @return  ResponseMessage
     */
    @Override
    @RequestMapping("updateProduct")
    @ResponseBody
    public ResponseMessage updateProduct(@RequestBody Product product) {
        logger.info("ProductProviderController updateProduct is start ....param:product-->"+product);
        //判断下数据
        ResponseMessage responseMessage = new ResponseMessage();
        if (product==null) {
            responseMessage.setCode(StatusCode.ERROR);
            responseMessage.setMessage("ProductProviderController updateProduct param is null");
        }
        responseMessage = iProductProviderService.updateProduct(product);
        logger.debug("ProductProviderController updateProduct result:responseMessage"+responseMessage);
        return responseMessage;
    }


}
